package com.ihg.hiex.admin.service.session.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;
import com.ihg.hiex.admin.dao.session.SessionNotificationDao;
import com.ihg.hiex.admin.dao.session.SessionTaskDao;
import com.ihg.hiex.admin.service.DataServiceImpl;
import com.ihg.hiex.admin.service.session.SessionTaskService;
import com.ihg.hiex.entity.data.conference.ConferenceRoom;
import com.ihg.hiex.entity.session.Session;
import com.ihg.hiex.entity.session.SessionNotification;
import com.ihg.hiex.entity.session.before.test.PreWorkAssignment;
import com.ihg.hiex.entity.session.meeting.SessionMeeting;
import com.ihg.hiex.entity.session.meeting.Target;
import com.ihg.hiex.entity.session.participator.Type;
import com.ihg.hiex.entity.session.task.SessionTask;
import com.ihg.hiex.entity.session.task.Status;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.Delegate;

/**
 *
 * @author Ark
 *
 */
@Service("sessionTaskService")
public class SessionTaskServiceImpl extends DataServiceImpl<SessionTask> implements SessionTaskService {

    @Dao
    private SessionTaskDao sessionTaskDao;
    
    @Dao
    private SessionNotificationDao sessionNotificationDao;
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.SessionTaskService#listByName(com.ihg.hiex.entity.session.Session, java.lang.String)
     */
    public List<SessionTask> listByName(Session session, String name) {
        return sessionTaskDao.list(session, name);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#list(com.ihg.hiex.entity.session.Session, java.lang.String)
     */
    public List<SessionTask> list(Session session, String parent) {
        return sessionTaskDao.listByParent(session, parent);
    }

    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#list(com.ihg.hiex.entity.session.Session, java.lang.String, com.ihg.hiex.entity.user.Assessor)
     */
    public List<SessionTask> list(Session session, String parent, Assessor assessor) {
        return sessionTaskDao.listByParent(session, parent, assessor);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#list(com.ihg.hiex.entity.session.Session, java.lang.String, com.ihg.hiex.entity.user.Delegate)
     */
    public List<SessionTask> list(Session session, String parent, Delegate delegate) {
        return sessionTaskDao.listByParent(session, parent, delegate);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#list(com.ihg.hiex.entity.session.Session, java.lang.Long[], java.lang.String[])
     */
    public Map<String, Map<String, SessionTask>> list(Session session, Long[] assessorIds, String[] names) {
        List<SessionTask> sessionTasks = sessionTaskDao.list(session, assessorIds, names);
        Map<String, List<SessionTask>> tasks = CollectionUtils.constructMapValues(sessionTasks, "delegate.id");
        
        Map<String, Map<String, SessionTask>> result = new HashMap<String, Map<String, SessionTask>>();
        for(Entry<String, List<SessionTask>> task : tasks.entrySet()) {
            Map<String, SessionTask> sessionTask = CollectionUtils.constructMapValue(task.getValue(), "name");
            result.put(task.getKey(), sessionTask);
        }
        return result;
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionTaskService#list(com.ihg.hiex.entity.session.Session, java.lang.String[], com.ihg.hiex.entity.session.participator.Type)
     */
    public Map<String, Map<String, Map<String, SessionTask>>> list(Session session, String[] parents, Type target) {
        List<SessionTask> sessionTasks = sessionTaskDao.list(session, parents, target);
        Map<String, List<SessionTask>> tasks = CollectionUtils.constructMapValues(sessionTasks, "parent");
        
        Map<String, Map<String, Map<String, SessionTask>>> result = new HashMap<String, Map<String, Map<String, SessionTask>>>();
        for(Entry<String, List<SessionTask>> task : tasks.entrySet()) {
            Map<String, List<SessionTask>> sessionTask = CollectionUtils.constructMapValues(task.getValue(), "delegate.id");
            
            Map<String, Map<String, SessionTask>> resultItem = new HashMap<String, Map<String, SessionTask>>();
            for(Entry<String, List<SessionTask>> st : sessionTask.entrySet()) {
                Map<String, SessionTask> rt = CollectionUtils.constructMapValue(st.getValue(), "name");
                resultItem.put(st.getKey(), rt);
            }
            result.put(task.getKey(), resultItem);
        }
        return result;
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionTaskService#listAssessorTask(com.ihg.hiex.entity.session.Session, java.lang.String[])
     */
    public Map<String, Map<String, Map<String, Map<String, SessionTask>>>> listAssessorTask(Session session,
            String[] parents) {
        List<SessionTask> sessionTasks = sessionTaskDao.list(session, parents, Type.ASSESSOR);
        Map<String, List<SessionTask>> tasks = CollectionUtils.constructMapValues(sessionTasks, "parent");
        
        Map<String, Map<String, Map<String, Map<String, SessionTask>>>> result = new HashMap<String, Map<String, Map<String, Map<String, SessionTask>>>>();
        for(Entry<String, List<SessionTask>> task : tasks.entrySet()) {
            Map<String, List<SessionTask>> sessionTask = CollectionUtils.constructMapValues(task.getValue(), "assessor.id");
            
            Map<String, Map<String, Map<String, SessionTask>>> rMap = new HashMap<String, Map<String, Map<String, SessionTask>>>();
            for(Entry<String, List<SessionTask>> st : sessionTask.entrySet()) {
                Map<String, List<SessionTask>> rt = CollectionUtils.constructMapValues(st.getValue(), "delegate.id");
                
                Map<String, Map<String, SessionTask>> resultItem = new HashMap<String, Map<String, SessionTask>>();
                for(Entry<String, List<SessionTask>> stMap : rt.entrySet()) {
                    Map<String, SessionTask> sMap = CollectionUtils.constructMapValue(st.getValue(), "name");
                    resultItem.put(stMap.getKey(), sMap);
                }
                rMap.put(st.getKey(), resultItem);
            }
            result.put(task.getKey(), rMap);
        }
        return result;
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionTaskService#listByName(com.ihg.hiex.entity.session.Session, java.lang.String[], com.ihg.hiex.entity.session.participator.Type)
     */
    public Map<String, Map<String, SessionTask>> listByName(Session session, String[] names, Type target) {
        List<SessionTask> sessionTasks = sessionTaskDao.listByName(session, names, target);
        Map<String, List<SessionTask>> tasks = target.isAssessor() ? CollectionUtils.constructMapValues(sessionTasks, "assessor.id") : CollectionUtils.constructMapValues(sessionTasks, "delegate.id");
        
        Map<String, Map<String, SessionTask>> result = new HashMap<String, Map<String, SessionTask>>();
        for(Entry<String, List<SessionTask>> task : tasks.entrySet()) {
            Map<String, SessionTask> resultItem = CollectionUtils.constructMapValue(task.getValue(), "name");
            result.put(task.getKey(), resultItem);
        }
        return result;
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#list(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Delegate, java.util.Date)
     */
    public Map<String, SessionTask> list(Session session, Delegate delegate, Date date) {
        
        Date startDate = DateUtils.parseDate(DateFormatUtils.format(date, "yyyy-MM-dd ") + "00:00:00", "yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtils.parseDate(DateFormatUtils.format(date, "yyyy-MM-dd ") + "23:23:59", "yyyy-MM-dd HH:mm:ss");
        
        List<SessionTask> sessionTasks = sessionTaskDao.list(session, delegate, startDate, endDate);
        List<SessionTask> adminTasks = sessionTaskDao.listAdmin(session, startDate, endDate);
        sessionTasks.addAll(adminTasks);
        
        return CollectionUtils.constructMapValue(sessionTasks, "name");
        
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionTaskService#listFinishTask(com.ihg.hiex.entity.session.Session, java.util.List, com.ihg.hiex.entity.session.participator.Type)
     */
    public Map<String, Integer> listFinishTask(Session session, List<String> names, Type target) {
            
        List<SessionTask> sessionTasks = sessionTaskDao.listByName(session, names, target);
        Map<String, List<SessionTask>> sessionTaskMap = com.demo2do.core.utils.CollectionUtils.constructMapValues(sessionTasks, "assessor.id");
        
        Map<String, Integer> result = new HashMap<String, Integer>();
        for (String assessorId : sessionTaskMap.keySet()) {
            
            List<SessionTask> tasks = sessionTaskMap.get(assessorId);
            int i = 0;
            for (SessionTask task : tasks) {
                if (task.getStatus().isFinish()) {
                    i++;
                }
            }
            
            result.put(assessorId, i);
            
        }
        
        return result;
            
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#list(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Delegate, java.util.Date)
     */
    public Map<String, SessionTask> list(Session session, Assessor assessor, Date date) {
        Date startDate = DateUtils.parseDate(DateFormatUtils.format(date, "yyyy-MM-dd ") + "00:00:00", "yyyy-MM-dd HH:mm:ss");
        Date endDate = DateUtils.parseDate(DateFormatUtils.format(date, "yyyy-MM-dd ") + "23:23:59", "yyyy-MM-dd HH:mm:ss");
        
        List<SessionTask> sessionTasks = sessionTaskDao.list(session, assessor, startDate, endDate);
        
        List<SessionTask> adminTasks = sessionTaskDao.listAdmin(session, startDate, endDate);
        
        sessionTasks.addAll(adminTasks);
        
        return CollectionUtils.constructMapValue(sessionTasks, "name");
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#list(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Assessor, com.ihg.hiex.entity.user.Delegate, java.lang.String, com.ihg.hiex.entity.session.participator.Type)
     */
    public Map<String, SessionTask> list(Session session, Assessor assessor, Delegate delegate,
            String parent, Type target) {
        List<SessionTask> sessionTasks = sessionTaskDao.list(session, assessor, delegate, parent, target);
        return CollectionUtils.constructMapValue(sessionTasks, "name");
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.SessionTaskService#listCalibrationReportStatus(java.util.List, com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.session.participator.Type, java.lang.String[], java.lang.String)
     */
    public Map<Long, Status> listCalibrationReportStatus(List<Delegate> delegates, Session session, Type type,
            String[] names, String parent) {
        
    	Map<Long, Status> result = new HashMap<Long, Status>();
    	if(session.getSessionPhase().isBefore()) {
            for(Delegate delegate : delegates) {
                result.put(delegate.getId(), Status.NOT_START);
            }
            
            return result;
    	}
    	
        List<SessionTask> sessionTasks = sessionTaskDao.listUnfinishTask(session, type, names, parent, Status.FINISH);
        Map<String, List<SessionTask>> tasks = CollectionUtils.constructMapValues(sessionTasks, "delegate.id");
        
        for(Delegate delegate : delegates) {
            if(MapUtils.isEmpty(tasks) || tasks.get(delegate.getIdString()) == null) {
                result.put(delegate.getId(), Status.FINISH);
            } else {
                result.put(delegate.getId(), Status.NOT_START);
            }
        }
        
        return result;
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.SessionTaskService#listByAssessor(com.ihg.hiex.entity.session.Session, java.util.List, java.util.Date)
     */
    public List<SessionTask> listMainTaskByAssessor(Session session, List<Assessor> assessors, Date date) {
        return sessionTaskDao.listMainTaskByAssessor(session, assessors, date);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.SessionTaskService#listItemTaskByAssessor(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Assessor, com.ihg.hiex.entity.user.Delegate, java.lang.String)
     */
    public List<SessionTask> listItemTaskByAssessor(Session session, Assessor assessor, Delegate delegate, String parent) {
        return sessionTaskDao.listItemTaskByAssessor(session, assessor, delegate, parent);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.SessionTaskService#listByDelegate(com.ihg.hiex.entity.session.Session, java.util.List, java.util.Date)
     */
    public List<SessionTask> listMainTaskByDelegate(Session session, List<Delegate> delegates, Date date) {
        return sessionTaskDao.listMainTaskByDelegate(session, delegates, date);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.SessionTaskService#listItemTaskByDelegate(com.ihg.hiex.entity.session.Session, java.util.List, java.lang.String)
     */
    public List<SessionTask> listItemTaskByDelegate(Session session, Delegate delegate, String parent) {
        return sessionTaskDao.listItemTaskByDelegate(session, delegate, parent);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#load(com.ihg.hiex.entity.session.Session, java.lang.String)
     */
    public SessionTask load(Session session, String name) {
        return sessionTaskDao.load(session, name);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#load(com.ihg.hiex.entity.session.Session, java.lang.String)
     */
    public SessionTask load(Session session, Type type, String name) {
        return sessionTaskDao.load(session, name, type);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#load(com.ihg.hiex.entity.session.Session, java.lang.String, com.ihg.hiex.entity.user.Assessor)
     */
    public SessionTask load(Session session, String name, Assessor assessor) {
        return sessionTaskDao.load(session, assessor, name);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#load(com.ihg.hiex.entity.session.Session, java.lang.String, com.ihg.hiex.entity.user.Delegate)
     */
    public SessionTask load(Session session, String name, Delegate delegate) {
        return sessionTaskDao.load(session, delegate, name);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#load(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Assessor, com.ihg.hiex.entity.user.Delegate, com.ihg.hiex.entity.session.participator.Type, java.lang.String)
     */
    public SessionTask load(Session session, Assessor assessor, Delegate delegate, Type target, String name) {
        return sessionTaskDao.load(session, assessor, delegate, target, name);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#finishSpecial(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.session.task.SessionTask)
     */
    public void finishSpecial(Session session, SessionTask sessionTask) {
        
        // 1. finish session Task
        genericDaoSupport.update(sessionTask.status(com.ihg.hiex.entity.session.task.Status.FINISH));

        // 2. check parent task, whether finish parent sessionTask
        this.finishSpecialParentTask(session, sessionTask);
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#loadPreWorkAssignment(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Delegate)
     */
    public PreWorkAssignment loadPreWorkAssignment(Session session, Delegate delegate) {
        return sessionTaskDao.load(session, delegate);
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#createPreWorkAssignment(com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.session.task.SessionTask, com.ihg.hiex.entity.session.before.test.PreWorkAssignment, java.util.List)
     */
    public void createPreWorkAssignment(Session session, Delegate delegate, SessionTask sessionTask, PreWorkAssignment preWorkAssignment) {
        
        // 1. save preWorkAssigment
        genericDaoSupport.save(preWorkAssignment.bind(session, delegate));
        
        // 2. finish sessionTask
        this.finish(session, sessionTask);
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#editPreWorkAssignment(com.ihg.hiex.entity.session.before.test.PreWorkAssignment)
     */
    public void editPreWorkAssignment(PreWorkAssignment preWorkAssignment) {
        genericDaoSupport.update(preWorkAssignment);
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionTaskService#editOrientationTask(com.ihg.hiex.entity.session.meeting.SessionMeeting)
     */
    public void editOrientationTask(SessionMeeting sessionMeeting, Long hostId, ConferenceRoom room, String startTime, String endTime) {
      
      // 1. update meeting
      genericDaoSupport.save(sessionMeeting.edit(hostId, room, startTime, endTime));
      
      String endMettingDay = DateFormatUtils.format(org.apache.commons.lang.time.DateUtils.addDays(sessionMeeting.getEndTime(), 1), "yyyy-MM-dd");
      Date taskEndDate = DateUtils.parseDate(endMettingDay + " " + "00:00:00", "yyyy-MM-dd HH:mm:SS");
      Date notificationTime = DateUtils.parseDate(endMettingDay + " " + "13:00:00", "yyyy-MM-dd HH:mm:SS");
      
      // 2. udpate orientation call
      if(sessionMeeting.getName().equals("Orientation Call") && sessionMeeting.getTarget().equals(Target.ADMIN_ASSESSORS)) {
        
        List<SessionTask> tasks = sessionTaskDao.listParentAndChild(sessionMeeting.getSession(), "Assessor Orientation");
        for(SessionTask task : tasks) {
          genericDaoSupport.update(task.editDate(sessionMeeting.getStartTime(), sessionMeeting.getEndTime()));
        }
        
        List<SessionTask> whoIknowTasks = sessionTaskDao.list(sessionMeeting.getSession(), "Who I Know");
        for(SessionTask task : whoIknowTasks) {
          genericDaoSupport.update(task.editDate(null, taskEndDate));
        }
        
        List<SessionNotification> notifications = sessionNotificationDao.list(sessionMeeting.getSession(), "whoIKnow", false);
        for(SessionNotification notification : notifications) {
          genericDaoSupport.update(notification.editTime(notificationTime));
        }
        
      } else if (sessionMeeting.getName().equals("Orientation Call") && sessionMeeting.getTarget().equals(Target.ADMIN_DELEGATES)){
        
        List<SessionTask> tasks = sessionTaskDao.listParentAndChild(sessionMeeting.getSession(), "Delegate Orientation");
        
        for(SessionTask task : tasks) {
           genericDaoSupport.update(task.editDate(sessionMeeting.getStartTime(), sessionMeeting.getEndTime()));
        }
      }
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionTaskService#editQAndACallTask(com.ihg.hiex.entity.session.meeting.SessionMeeting, java.lang.Long, com.ihg.hiex.entity.data.conference.ConferenceRoom, java.lang.String, java.lang.String)
     */
    public void editQAndACallTask(SessionMeeting sessionMeeting, Long hostId, ConferenceRoom room, String startTime, String endTime) {
        
        // 1. update meeting
        genericDaoSupport.save(sessionMeeting.edit(hostId, room, startTime, endTime));
        
        // 2. udpate orientation call
        if(sessionMeeting.getName().equals("Q&A Call") && sessionMeeting.getTarget().equals(Target.ADMIN_ASSESSORS)) {
          List<SessionTask> tasks = sessionTaskDao.list(sessionMeeting.getSession(), "Assessor Q&A Call");
          
          for(SessionTask task : tasks) {
            genericDaoSupport.update(task.editDate(sessionMeeting.getStartTime(), sessionMeeting.getEndTime()));
          }
        } else if (sessionMeeting.getName().equals("Q&A Call") && sessionMeeting.getTarget().equals(Target.ADMIN_DELEGATES)){
          List<SessionTask> tasks = sessionTaskDao.list(sessionMeeting.getSession(), "Delegate Q&A Call");
          for(SessionTask task : tasks) {
            genericDaoSupport.update(task.editDate(sessionMeeting.getStartTime(), sessionMeeting.getEndTime()));
          }
        }
    }
    
    /**
     * @param session
     * @param sessionTask
     */
    private void finishSpecialParentTask(Session session, SessionTask sessionTask) {
        
        // 1. 判断父Task是否存在
        String parentTaskName = sessionTask.getParent();
        if (StringUtils.isBlank(parentTaskName)) {
            return;
        }
        
        // 2. Finish  Assessor父Task
        List<SessionTask> childSessionTasks = sessionTaskDao.listByParent(session, parentTaskName);
        int i = 0;
        for (SessionTask childTask : childSessionTasks) {
            
            if (childTask.getStatus().isFinish()) {
                i++;
            }
            
            if (childSessionTasks.size() == i) {
                
                SessionTask parentTask = sessionTaskDao.load(session, parentTaskName);
                genericDaoSupport.update(parentTask.status(com.ihg.hiex.entity.session.task.Status.FINISH));
                
                this.finishSpecial(session, parentTask);
                
            }
            
        }
            
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.assessment.service.session.SessionTaskService#finish(com.ihg.hiex.entity.session.task.SessionTask[], com.ihg.hiex.entity.session.Session, com.ihg.hiex.entity.user.Assessor, com.ihg.hiex.entity.user.Delegate, com.ihg.hiex.entity.session.participator.Type, java.lang.String)
     */
    public void finish(SessionTask[] sessionTasks, Session session, Assessor assessor, Delegate delegate, Type target, String parent) {
        // update scoring task first
        for(SessionTask sessionTask : sessionTasks) {
            genericDaoSupport.update(sessionTask.status(Status.FINISH));
        }
        
        // if all tasks had finished then finish meeting task
        List<SessionTask> unfinishTasks = sessionTaskDao.list(session, assessor, delegate, target, parent, Status.FINISH);
        if(unfinishTasks.isEmpty()) {
            SessionTask parentTask = sessionTaskDao.load(session, assessor, delegate, target, parent);
            genericDaoSupport.update(parentTask.status(Status.FINISH));
        }
        
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.SessionTaskService#finish(com.ihg.hiex.entity.session.task.SessionTask)
     */
    public void finish(SessionTask sessionTask) {
        genericDaoSupport.update(sessionTask.status(Status.FINISH));
    }
    
    /* (non-Javadoc)
     * @see com.ihg.hiex.admin.service.SessionTaskService#construct(com.ihg.hiex.entity.session.Session, java.util.List)
     */
    public Map<String, SessionTask> construct(Session session, List<String> names) {
        
        List<SessionTask> tasks = sessionTaskDao.list(session, names);
        Map<String, SessionTask> result = new HashMap<String, SessionTask>();
        
        for (SessionTask task : tasks) {
            String taskName = task.getName();
            if (!result.containsKey(taskName)) {
                result.put(taskName, task);
            }
        }
        
        return result;
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.ihg.hiex.admin.service.session.SessionTaskService#finishEvaluation(com.ihg.hiex.entity.session.task.SessionTask)
     */
    public void finishEvaluation(SessionTask sessionTask) {
        genericDaoSupport.update(sessionTask.status(Status.FINISH));
        
        // finish parent task
        List<SessionTask> tasks = sessionTaskDao.listUnfinishTask(sessionTask.getSession(), Type.DELEGATE, sessionTask.getDelegate(), "Evaluation", Status.FINISH);
        if(tasks.isEmpty()) {
            SessionTask parentTask = sessionTaskDao.load(sessionTask.getSession(), sessionTask.getDelegate(), "Evaluation");
            genericDaoSupport.update(parentTask.status(Status.FINISH));
        }
        
    }

    @Override
    public void finish(Session session, SessionTask sessionTask) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void uploadCareerWorkbook(Session session, SessionTask sessionTask, MultipartFile workbook) {
        // TODO Auto-generated method stub
        
    }

}
